home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.plaf.basic;
-
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.IllegalComponentStateException;
- import java.awt.Point;
- import java.awt.Window;
- import java.awt.event.ContainerListener;
- import java.awt.event.FocusListener;
- import java.awt.event.WindowListener;
- import java.beans.PropertyChangeListener;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.JToolBar;
- import javax.swing.KeyStroke;
- import javax.swing.LookAndFeel;
- import javax.swing.SwingConstants;
- import javax.swing.UIManager;
- import javax.swing.event.MouseInputListener;
- import javax.swing.plaf.ComponentUI;
- import javax.swing.plaf.ToolBarUI;
- import javax.swing.plaf.UIResource;
-
- public class BasicToolBarUI extends ToolBarUI implements SwingConstants {
- protected JToolBar toolBar;
- private boolean floating;
- private int floatingX;
- private int floatingY;
- private JFrame floatingFrame;
- protected DragWindow dragWindow;
- private Container dockingSource;
- private int dockingSensitivity = 0;
- protected int focusedCompIndex = -1;
- protected Color dockingColor = null;
- protected Color floatingColor = null;
- protected Color dockingBorderColor = null;
- protected Color floatingBorderColor = null;
- protected MouseInputListener dockingListener;
- protected PropertyChangeListener propertyListener;
- protected ContainerListener toolBarContListener;
- protected FocusListener toolBarFocusListener;
- protected KeyStroke upKey;
- protected KeyStroke downKey;
- protected KeyStroke leftKey;
- protected KeyStroke rightKey;
- private KeyStroke kpUpKey;
- private KeyStroke kpDownKey;
- private KeyStroke kpLeftKey;
- private KeyStroke kpRightKey;
- private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";
-
- public boolean canDock(Component var1, Point var2) {
- boolean var3 = false;
- if (var1.contains(var2)) {
- if (this.dockingSensitivity == 0) {
- this.dockingSensitivity = this.toolBar.getSize().height;
- }
-
- if (var2.y < this.dockingSensitivity) {
- var3 = true;
- }
-
- if (var2.y > var1.getSize().height - this.dockingSensitivity) {
- var3 = true;
- }
-
- if (var2.x < this.dockingSensitivity) {
- var3 = true;
- }
-
- if (var2.x > var1.getSize().width - this.dockingSensitivity) {
- var3 = true;
- }
- }
-
- return var3;
- }
-
- protected MouseInputListener createDockingListener() {
- return new DockingListener(this, this.toolBar);
- }
-
- protected DragWindow createDragWindow(JToolBar var1) {
- Object var2 = null;
- if (this.toolBar != null) {
- Container var3;
- for(var3 = this.toolBar.getParent(); var3 != null && !(var3 instanceof Frame); var3 = ((Component)var3).getParent()) {
- }
-
- if (var3 != null && var3 instanceof Frame) {
- Frame var4 = (Frame)var3;
- }
- }
-
- if (this.floatingFrame == null) {
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- }
-
- JFrame var5 = this.floatingFrame;
- DragWindow var6 = new DragWindow(this, var5);
- return var6;
- }
-
- protected JFrame createFloatingFrame(JToolBar var1) {
- JFrame var2 = new JFrame(((Component)var1).getName());
- ((Frame)var2).setResizable(false);
- WindowListener var3 = this.createFrameListener();
- ((Window)var2).addWindowListener(var3);
- return var2;
- }
-
- protected WindowListener createFrameListener() {
- return new FrameListener(this);
- }
-
- protected PropertyChangeListener createPropertyListener() {
- return new PropertyListener(this);
- }
-
- protected ContainerListener createToolBarContListener() {
- return new ToolBarContListener(this);
- }
-
- protected FocusListener createToolBarFocusListener() {
- return new ToolBarFocusListener(this);
- }
-
- public static ComponentUI createUI(JComponent var0) {
- return new BasicToolBarUI();
- }
-
- protected void dragTo(Point var1, Point var2) {
- if (this.toolBar.isFloatable()) {
- try {
- if (this.dragWindow == null) {
- this.dragWindow = this.createDragWindow(this.toolBar);
- }
-
- Point var3 = this.dragWindow.getOffset();
- if (var3 == null) {
- Dimension var4 = this.toolBar.getPreferredSize();
- var3 = new Point(var4.width / 2, var4.height / 2);
- this.dragWindow.setOffset(var3);
- }
-
- Point var11 = new Point(var2.x + var1.x, var2.y + var1.y);
- Point var5 = new Point(var11.x - var3.x, var11.y - var3.y);
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- }
-
- Point var6 = this.dockingSource.getLocationOnScreen();
- Point var7 = new Point(var11.x - var6.x, var11.y - var6.y);
- if (this.canDock(this.dockingSource, var7)) {
- this.dragWindow.setBackground(this.getDockingColor());
- String var8 = this.getDockingConstraint(this.dockingSource, var7);
- int var9 = this.mapConstraintToOrientation(var8);
- this.dragWindow.setOrientation(var9);
- this.dragWindow.setBorderColor(this.dockingBorderColor);
- } else {
- this.dragWindow.setBackground(this.getFloatingColor());
- this.dragWindow.setOrientation(0);
- this.dragWindow.setBorderColor(this.floatingBorderColor);
- }
-
- this.dragWindow.setLocation(var5.x, var5.y);
- if (!this.dragWindow.isVisible()) {
- Dimension var12 = this.toolBar.getPreferredSize();
- this.dragWindow.setSize(var12.width, var12.height);
- this.dragWindow.show();
- }
- } catch (IllegalComponentStateException var10) {
- }
- }
-
- }
-
- protected void floatAt(Point var1, Point var2) {
- if (this.toolBar.isFloatable()) {
- try {
- Point var3 = this.dragWindow.getOffset();
- if (var3 == null) {
- var3 = var1;
- this.dragWindow.setOffset(var1);
- }
-
- Point var4 = new Point(var2.x + var1.x, var2.y + var1.y);
- this.setFloatingLocation(var4.x - var3.x, var4.y - var3.y);
- if (this.dockingSource != null) {
- Point var5 = this.dockingSource.getLocationOnScreen();
- Point var6 = new Point(var4.x - var5.x, var4.y - var5.y);
- if (this.canDock(this.dockingSource, var6)) {
- this.setFloating(false, var6);
- } else {
- this.setFloating(true, (Point)null);
- }
- } else {
- this.setFloating(true, (Point)null);
- }
-
- this.dragWindow.setOffset((Point)null);
- } catch (IllegalComponentStateException var7) {
- }
- }
-
- }
-
- public Color getDockingColor() {
- return this.dockingColor;
- }
-
- private String getDockingConstraint(Component var1, Point var2) {
- String var3 = "North";
- if (var2 != null && var1.contains(var2)) {
- if (this.dockingSensitivity == 0) {
- this.dockingSensitivity = this.toolBar.getSize().height;
- }
-
- if (var2.y > var1.getSize().height - this.dockingSensitivity) {
- var3 = "South";
- }
-
- if (var2.x < this.dockingSensitivity) {
- var3 = "West";
- }
-
- if (var2.x > var1.getSize().width - this.dockingSensitivity) {
- var3 = "East";
- }
-
- if (var2.y < this.dockingSensitivity) {
- var3 = "North";
- }
- }
-
- return var3;
- }
-
- public Color getFloatingColor() {
- return this.floatingColor;
- }
-
- public Dimension getMaximumSize(JComponent var1) {
- return this.getPreferredSize(var1);
- }
-
- public Dimension getMinimumSize(JComponent var1) {
- return this.getPreferredSize(var1);
- }
-
- public Dimension getPreferredSize(JComponent var1) {
- return null;
- }
-
- protected void installComponents() {
- }
-
- protected void installDefaults() {
- LookAndFeel.installBorder(this.toolBar, "ToolBar.border");
- LookAndFeel.installColorsAndFont(this.toolBar, "ToolBar.background", "ToolBar.foreground", "ToolBar.font");
- if (this.dockingColor == null || this.dockingColor instanceof UIResource) {
- this.dockingColor = UIManager.getColor("ToolBar.dockingBackground");
- }
-
- if (this.floatingColor == null || this.floatingColor instanceof UIResource) {
- this.floatingColor = UIManager.getColor("ToolBar.floatingBackground");
- }
-
- if (this.dockingBorderColor == null || this.dockingBorderColor instanceof UIResource) {
- this.dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");
- }
-
- if (this.floatingBorderColor == null || this.floatingBorderColor instanceof UIResource) {
- this.floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");
- }
-
- }
-
- protected void installKeyboardActions() {
- UpAction var1 = new UpAction(this);
- DownAction var2 = new DownAction(this);
- LeftAction var3 = new LeftAction(this);
- RightAction var4 = new RightAction(this);
- this.upKey = KeyStroke.getKeyStroke(38, 0);
- this.downKey = KeyStroke.getKeyStroke(40, 0);
- this.leftKey = KeyStroke.getKeyStroke(37, 0);
- this.rightKey = KeyStroke.getKeyStroke(39, 0);
- this.kpUpKey = KeyStroke.getKeyStroke("KP_UP");
- this.kpDownKey = KeyStroke.getKeyStroke("KP_DOWN");
- this.kpLeftKey = KeyStroke.getKeyStroke("KP_LEFT");
- this.kpRightKey = KeyStroke.getKeyStroke("KP_RIGHT");
- this.toolBar.registerKeyboardAction(var1, this.upKey, 1);
- this.toolBar.registerKeyboardAction(var2, this.downKey, 1);
- this.toolBar.registerKeyboardAction(var3, this.leftKey, 1);
- this.toolBar.registerKeyboardAction(var4, this.rightKey, 1);
- this.toolBar.registerKeyboardAction(var1, this.kpUpKey, 1);
- this.toolBar.registerKeyboardAction(var2, this.kpDownKey, 1);
- this.toolBar.registerKeyboardAction(var3, this.kpLeftKey, 1);
- this.toolBar.registerKeyboardAction(var4, this.kpRightKey, 1);
- }
-
- protected void installListeners() {
- this.dockingListener = this.createDockingListener();
- if (this.dockingListener != null) {
- this.toolBar.addMouseMotionListener(this.dockingListener);
- this.toolBar.addMouseListener(this.dockingListener);
- }
-
- this.propertyListener = this.createPropertyListener();
- this.toolBarContListener = this.createToolBarContListener();
- if (this.toolBarContListener != null) {
- this.toolBar.addContainerListener(this.toolBarContListener);
- }
-
- this.toolBarFocusListener = this.createToolBarFocusListener();
- if (this.toolBarFocusListener != null) {
- Component[] var1 = this.toolBar.getComponents();
-
- for(int var2 = 0; var2 < var1.length; ++var2) {
- var1[var2].addFocusListener(this.toolBarFocusListener);
- }
- }
-
- }
-
- public void installUI(JComponent var1) {
- this.toolBar = (JToolBar)var1;
- this.installDefaults();
- this.installComponents();
- this.installListeners();
- this.installKeyboardActions();
- this.dockingSensitivity = 0;
- this.floating = false;
- this.floatingX = this.floatingY = 0;
- this.floatingFrame = null;
- this.setOrientation(this.toolBar.getOrientation());
- var1.setOpaque(true);
- if (var1.getClientProperty(FOCUSED_COMP_INDEX) != null) {
- this.focusedCompIndex = (Integer)var1.getClientProperty(FOCUSED_COMP_INDEX);
- }
-
- }
-
- public boolean isFloating() {
- return this.floating;
- }
-
- private int mapConstraintToOrientation(String var1) {
- int var2 = this.toolBar.getOrientation();
- if (var1 != null) {
- if (!var1.equals("East") && !var1.equals("West")) {
- if (var1.equals("North") || var1.equals("South")) {
- var2 = 0;
- }
- } else {
- var2 = 1;
- }
- }
-
- return var2;
- }
-
- protected void navigateFocusedComp(int var1) {
- int var2 = this.toolBar.getComponentCount();
- switch (var1) {
- case 1:
- case 7:
- if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
- int var5 = this.focusedCompIndex - 1;
-
- while(var5 != this.focusedCompIndex) {
- if (var5 < 0) {
- var5 = var2 - 1;
- }
-
- Component var6 = this.toolBar.getComponentAtIndex(var5--);
- if (var6 != null && var6.isFocusTraversable()) {
- var6.requestFocus();
- break;
- }
- }
- }
- case 2:
- case 4:
- case 6:
- default:
- break;
- case 3:
- case 5:
- if (this.focusedCompIndex >= 0 && this.focusedCompIndex < var2) {
- int var3 = this.focusedCompIndex + 1;
-
- while(var3 != this.focusedCompIndex) {
- if (var3 >= var2) {
- var3 = 0;
- }
-
- Component var4 = this.toolBar.getComponentAtIndex(var3++);
- if (var4 != null && var4.isFocusTraversable()) {
- var4.requestFocus();
- break;
- }
- }
- }
- }
-
- }
-
- public void setDockingColor(Color var1) {
- this.dockingColor = var1;
- }
-
- public void setFloating(boolean var1, Point var2) {
- if (this.toolBar.isFloatable()) {
- if (this.dragWindow != null) {
- this.dragWindow.setVisible(false);
- }
-
- this.floating = var1;
- if (var1) {
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- this.dockingSource.remove(this.toolBar);
- }
-
- if (this.propertyListener != null) {
- UIManager.addPropertyChangeListener(this.propertyListener);
- }
-
- if (this.floatingFrame == null) {
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- }
-
- this.floatingFrame.getContentPane().add(this.toolBar, "Center");
- this.setOrientation(0);
- this.floatingFrame.pack();
- this.floatingFrame.setLocation(this.floatingX, this.floatingY);
- this.floatingFrame.show();
- } else {
- if (this.floatingFrame == null) {
- this.floatingFrame = this.createFloatingFrame(this.toolBar);
- }
-
- this.floatingFrame.setVisible(false);
- this.floatingFrame.getContentPane().remove(this.toolBar);
- String var3 = this.getDockingConstraint(this.dockingSource, var2);
- int var4 = this.mapConstraintToOrientation(var3);
- this.setOrientation(var4);
- if (this.dockingSource == null) {
- this.dockingSource = this.toolBar.getParent();
- }
-
- if (this.propertyListener != null) {
- UIManager.removePropertyChangeListener(this.propertyListener);
- }
-
- this.dockingSource.add(var3, this.toolBar);
- }
-
- this.dockingSource.invalidate();
- Container var5 = this.dockingSource.getParent();
- if (var5 != null) {
- var5.validate();
- }
-
- this.dockingSource.repaint();
- }
-
- }
-
- public void setFloatingColor(Color var1) {
- this.floatingColor = var1;
- }
-
- public void setFloatingLocation(int var1, int var2) {
- this.floatingX = var1;
- this.floatingY = var2;
- }
-
- public void setOrientation(int var1) {
- this.toolBar.setOrientation(var1);
- if (this.dragWindow != null) {
- this.dragWindow.setOrientation(var1);
- }
-
- }
-
- protected void uninstallComponents() {
- }
-
- protected void uninstallDefaults() {
- LookAndFeel.uninstallBorder(this.toolBar);
- this.dockingColor = null;
- this.floatingColor = null;
- this.dockingBorderColor = null;
- this.floatingBorderColor = null;
- }
-
- protected void uninstallKeyboardActions() {
- this.toolBar.unregisterKeyboardAction(this.upKey);
- this.toolBar.unregisterKeyboardAction(this.downKey);
- this.toolBar.unregisterKeyboardAction(this.leftKey);
- this.toolBar.unregisterKeyboardAction(this.rightKey);
- this.upKey = this.downKey = this.rightKey = this.leftKey = null;
- this.toolBar.unregisterKeyboardAction(this.kpUpKey);
- this.toolBar.unregisterKeyboardAction(this.kpDownKey);
- this.toolBar.unregisterKeyboardAction(this.kpLeftKey);
- this.toolBar.unregisterKeyboardAction(this.kpRightKey);
- this.kpUpKey = this.kpDownKey = this.kpRightKey = this.kpLeftKey = null;
- }
-
- protected void uninstallListeners() {
- if (this.dockingListener != null) {
- this.toolBar.removeMouseMotionListener(this.dockingListener);
- this.toolBar.removeMouseListener(this.dockingListener);
- this.dockingListener = null;
- }
-
- if (this.propertyListener != null) {
- this.propertyListener = null;
- }
-
- if (this.toolBarContListener != null) {
- this.toolBar.removeContainerListener(this.toolBarContListener);
- this.toolBarContListener = null;
- }
-
- if (this.toolBarFocusListener != null) {
- Component[] var1 = this.toolBar.getComponents();
-
- for(int var2 = 0; var2 < var1.length; ++var2) {
- var1[var2].removeFocusListener(this.toolBarFocusListener);
- }
-
- this.toolBarFocusListener = null;
- }
-
- }
-
- public void uninstallUI(JComponent var1) {
- this.uninstallDefaults();
- this.uninstallComponents();
- this.uninstallListeners();
- this.uninstallKeyboardActions();
- if (this.isFloating()) {
- this.setFloating(false, (Point)null);
- }
-
- this.floatingFrame = null;
- this.dragWindow = null;
- this.dockingSource = null;
- var1.putClientProperty(FOCUSED_COMP_INDEX, new Integer(this.focusedCompIndex));
- }
- }
-